package com.td.module.agro.controller.app.inputreview;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.agro.controller.admin.inputreview.vo.*;
import com.td.module.agro.convert.inputreview.InputReviewConvert;
import com.td.module.agro.dal.dataobject.inputreview.InputReviewDO;
import com.td.module.agro.service.inputreview.InputReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "用户APP - 商品与投入品中间")
@RestController
@RequestMapping("/agro/input-review")
@Validated
public class AppInputReviewController {

    @Resource
    private InputReviewService inputReviewService;

    @PostMapping("/create")
    @ApiOperation("创建商品与投入品中间")
    public CommonResult<Long> createInputReview(@Valid @RequestBody InputReviewCreateReqVO createReqVO) {
        return success(inputReviewService.createInputReview(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新商品与投入品中间")
    public CommonResult<Boolean> updateInputReview(@Valid @RequestBody InputReviewUpdateReqVO updateReqVO) {
        inputReviewService.updateInputReview(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商品与投入品中间")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteInputReview(@RequestParam("id") Long id) {
        inputReviewService.deleteInputReview(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商品与投入品中间")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<InputReviewRespVO> getInputReview(@RequestParam("id") Long id) {
        InputReviewDO inputReview = inputReviewService.getInputReview(id);
        return success(InputReviewConvert.INSTANCE.convert(inputReview));
    }

    @GetMapping("/list")
    @ApiOperation("获得商品与投入品中间列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<InputReviewRespVO>> getInputReviewList(@RequestParam("ids") Collection<Long> ids) {
        List<InputReviewDO> list = inputReviewService.getInputReviewList(ids);
        return success(InputReviewConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得商品与投入品中间分页")
    public CommonResult<PageResult<InputReviewRespVO>> getInputReviewPage(@Valid InputReviewPageReqVO pageVO) {
        PageResult<InputReviewDO> pageResult = inputReviewService.getInputReviewPage(pageVO);
        return success(InputReviewConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出商品与投入品中间 Excel")
    @OperateLog(type = EXPORT)
    public void exportInputReviewExcel(@Valid InputReviewExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InputReviewDO> list = inputReviewService.getInputReviewList(exportReqVO);
        // 导出 Excel
        List<InputReviewExcelVO> datas = InputReviewConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品与投入品中间.xls", "数据", InputReviewExcelVO.class, datas);
    }

}
